from odoo.http import request, Response
import functools
import json
from odoo.exceptions import ValidationError


def create_records(result_json):
    """
    ret: 接口请求的request
    data: 接口json字串，
    result_json: 接口返回json字串，注意这里是json.dumps(result, ensure_ascii=False) 处理之后的结果
    state：success请求成功， fail请求失败
    """
    base_url = request.httprequest.base_url
    data = request.httprequest.get_json()
    token = data.get('token', False)
    api_user_obj = request.env['api.users'].sudo().search([('token', '=', token)], limit=1)
    api_result = json.loads(result_json)['data']
    api_records = {
        'api_params': data,
        'api_user_id': api_user_obj.id,
        'api_route': base_url,
        'state': 'fail' if api_result['code'] == -1 else 'success',
        'result_json': result_json
    }
    request.env['api.records'].sudo().create(api_records)
